package com.xxl.vueadmin.security;

import com.xxl.vueadmin.entity.SysUser;
import com.xxl.vueadmin.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @version 1.0
 * @Author: 许啸林 用户查询实现类
 * @Date: 2021/7/31 13:38
 */
@Service()
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    ISysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getBayUserName(userName);
        if(sysUser == null) {
            throw new UsernameNotFoundException("用户名或密码不正确");
        }
        String userId = sysUser.getId();
        return new AccountUser(userId, sysUser.getUsername(), sysUser.getPassword(), generanteGrantList(userId));
    }

    /**
     * 获取权限信息
     * @param userId
     * @return
     */
    public List<GrantedAuthority> generanteGrantList(String userId) {
        String Authority = sysUserService.gerUserAuthority(userId);
        return AuthorityUtils.commaSeparatedStringToAuthorityList(Authority);
    }
}
